<template><!-- getRobotListByParentKey -->
  <LeftTree ref="leftTree" :handleEdit="handleEdit" :getListFn="commonService.getRobotListByParentKey"
    :rowClick="rowClick" v-model:filterText="filterText" type="cate" :dictionaryKey="props.dictionaryKey">
    <template #title> 分类 </template>
    <template #input>
      <el-input v-model="filterText" placeholder="请输入分类名称"></el-input>
    </template>
    <template #button>
      <el-button style="width: 100%; margin-top: 10px" type="primary" @click="handleEdit({})">
        <el-icon>
          <Plus />
        </el-icon>
        添加分类
      </el-button>
    </template>
    <template #table>
      <template
        v-if="['commonManager', 'cruxKeyManager', 'sensitiveManager','opinionKeyManager'].includes(props.dictionaryKey)">

        <el-table-column prop="dictionaryName" label="分类名称">
          <template #default="scope">
            <span>
              {{ scope.row.dictionaryName }} ({{ scope.row.enableNum }})
            </span>
          </template>
        </el-table-column>
      </template>
      <template v-else>
        <el-table-column prop="dictionaryValue" label="分类ID" />
        <el-table-column prop="dictionaryName" label="分类名称"></el-table-column>
      </template>

    </template>
  </LeftTree>
  <CateEditForm v-if="visibleCateEditForm" v-model:dialogVisible="visibleCateEditForm" :item="item"
    :handleSearch="handleSearch"></CateEditForm>
</template>

<script setup lang="ts">
//import CateEditForm from '@/views/systemSet/dataDictionary/EditForm.vue'
import CateEditForm from './CateEditForm.vue'
import { commonService } from '@/api/common'
import LeftTree from '../leftTreeSlot.vue'
import { Debounce } from '@/hooks/throttle'
const visibleCateEditForm = ref(false)
const props = defineProps({
  search: {
    type: Function,
  },
  form: {
    type: Object,
  },
  dictionaryKey: {
    type: String,
  },
})
const item = ref({})
const filterText = ref('')
const leftTree = ref(null)
const parentId = ref(0);

const handleSearch = () => {
  leftTree.value.getCateList()
}
defineExpose({ handleSearch})
const handleEdit = (row) => {
  item.value = { ...row, parentId: parentId }

  visibleCateEditForm.value = true
}
const rowClick = (e) => {
  props.form.typeId = e?.dictionaryValue || ''
  props.search()
}
const getAllDictionaryList = async () => {
  try {
    commonService.getAllDictionaryList({
      data: { dictionaryKey: props.dictionaryKey },
    }).then((res) => {
      if (res.data && res.data.length > 0) {
        parentId.value = res.data[0].dictionaryId
      }
    })


  } catch (error) {

  }

}
onMounted(() => {

  getAllDictionaryList();
})
</script>
